const router = require('express').Router();
const joi = require('joi');
const joiValidate = require('../../../utils/joiValidate');
const accountService = require('../../../service/account/accountService');
const verifyUser = require('../../../utils/jwtHanlder');

/**
 * @api {get} /v1/user/account/info  获取用户账户信息
 * @apiVersion 1.0.0
 * @apiName UserAccount
 * @apiGroup  User
 *
 * @apiDescription 获取用户的账户信息
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/user/account/info
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 * @apiSuccess {Object} data    数据集合
 * @apiSuccess {Object} data.account    数据集合
 * @apiSuccess {Number} data.account.balance    余额
 * @apiSuccess {Number} data.account.balanceCash    可提现金额
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 *     {
          "code": 200,
          "message": "请求成功"，
          "data": {
              account: { balance: 0, balanceCash: 0 }
          }
       }
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.get('/info', verifyUser.verify(), (req, res, next) => {
  const { userId } = req.user;

  accountService.getAccountInfo({ userId })
    .then((data) => res.success(data))
    .catch(e => next(e))
});

/**
 * @api {get} /v1/user/account/bill  获取用户账单信息
 * @apiVersion 1.0.0
 * @apiName UserAccountBill
 * @apiGroup  User
 *
 * @apiDescription 获取用户账单信息
 *
 * @apiParam {number} [offset] 分页参数
 * @apiParam {Number} [limit] 分页参数
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/user/account/bill
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 * @apiSuccess {Object} data    数据集合
 * @apiSuccess {Object} data.list    数据集合
 * @apiSuccess {Number} data.list.amount    金额
 * @apiSuccess {Number} data.list.userId    用户Id
 * @apiSuccess {String} data.list.type    类型
 * @apiSuccess {String} data.list.createTime   账单时间
 * @apiSuccess {Number} data.count   数量
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功",
    "data": {
        "list": [
            {
                "amount": "12.00",
                "userId": 2,
                "type": "COURSE_BUY",
                "remark": "购买课程",
                "createTime": "2019-06-25 18:27:11"
            }
        ],
        "count": 5
    }
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.get('/bill', verifyUser.verify(), joiValidate(joi.object().keys({
  offset: joi.number().label('请传入分页参数'),
  limit: joi.number().label('请传入分页参数'),
})), (req, res, next) => {
  let { offset, limit } = req.query;
  const { userId } = req.user;
  offset = offset && Number(offset) || 0;
  limit = limit && Number(limit) || 20;
  accountService.getAccountBill({ userId, offset, limit })
    .then((data) => res.success(data))
    .catch(e => next(e))
});
//
// router.post('/withdraw', verifyUser.verify(), joiValidate(joi.object().keys({
//     amount: joi.number().required().label('请传入提现金额')
// })), (req, res, next) => {
//     let { amount } = req.body;
//     const { userId } = req.user;
//     accountService.withDraw({ userId, amount })
//         .then((data) => res.success(data))
//         .catch(e => next(e))
// })

module.exports = router;
